package com.chenjun.web.controller.sys;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chenjun.common.base.ResultDto;
import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.common.model.DataGridSortModel;
import com.chenjun.core.dao.generator.entity.SysRole;
import com.chenjun.core.dao.generator.entity.SysUserRole;
import com.chenjun.core.service.sys.SysRoleService;
import com.chenjun.core.service.sys.SysUserRoleService;
import com.chenjun.core.vo.sys.RoleVo;
import com.chenjun.web.controller.BaseController;

/**
 * 用户控制器
 * 
 * @Description: 角色控制器
 * @author yanjinyin
 * @date 2017年7月23日 下午4:09:05
 */
@SuppressWarnings("serial")
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysUserRoleService sysUserRoleService;

	/**
	 * 页面展示
	 * 
	 * @return ModelAndView
	 */
	@RequestMapping("/list.html")
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("role/list");
		return mv;
	}

	@RequestMapping("/roleResource/{id}.html")
	public ModelAndView indexRoleResource(@PathVariable String id) {
		ModelAndView mv = new ModelAndView("role/roleResource");
		mv.addObject("roleId", id);
		return mv;
	}

	/**
	 * 分页搜索
	 * 
	 * @param datagrid
	 * @return ResultDto
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 */
	@RequestMapping(value = "/search")
	@ResponseBody
	public ResultDto list(DataGridModel datagrid) {
		ResultDto rtDto = new ResultDto();
		datagrid.getSortBy().add(new DataGridSortModel("created_time", DataGridSortModel.DESC));
		Pagination data = sysRoleService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}

	@RequestMapping(value = "/searchAll")
	@ResponseBody
	public ResultDto listAll() {
		DataGridModel datagrid = new DataGridModel();
		datagrid.setStart(0);
		datagrid.setRows(0);
		ResultDto rtDto = new ResultDto();
		datagrid.getSortBy().add(new DataGridSortModel("created_time", DataGridSortModel.DESC));
		Pagination data = sysRoleService.doPagination(datagrid);
		rtDto.setData(data);
		return rtDto;
	}

	/**
	 * 编辑页面元素
	 * 
	 * @return ResultDto
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public ResultDto edit(HttpServletRequest request, @Valid RoleVo dto, Errors errors) throws IllegalAccessException,
			InvocationTargetException {
		ResultDto rtDto = new ResultDto();

		// 验证
		if (!hanleErrors(rtDto, errors)) {
			return rtDto;
		}

		DataGridModel dgm = new DataGridModel();
		dgm.getParams().put("name", dto.getName());
		dgm.getParams().put("nId", dto.getId());
		List<SysRole> roles = sysRoleService.doListByCondition(dgm);
		if (!CollectionUtils.isEmpty(roles)) {
			rtDto.resultWaring();
			rtDto.setMessage("该角色名已经存在");
			return rtDto;
		}

		int i = sysRoleService.createOrUpdate(RoleVo.getModelByDto(dto));
		if (i <= 0) {
			rtDto.resultError();
			rtDto.setMessage("编辑系统异常");
		}
		return rtDto;
	}

	/**
	 * 删除指定的记录
	 * 
	 * @param id
	 * @return ResultDto
	 */
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public ResultDto delete(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		SysRole role = sysRoleService.get(id);
		int result = 0;
		if (role != null) {
			DataGridModel dgm = new DataGridModel();
			dgm.getParams().put("roleId", id);// 判断此角色是否已使用
			List<SysUserRole> userRoles = sysUserRoleService.doListByCondition(dgm);
			if (!CollectionUtils.isEmpty(userRoles)) {
				rtDto.resultError();
				rtDto.setMessage("该角色已被使用");
				return rtDto;
			}
			result = sysRoleService.delete(id);
		}
		if (result <= 0) {
			rtDto.resultError();
			rtDto.setMessage("要删除的记录不存在!");
		}
		return rtDto;
	}

	/**
	 * 根据参数获取信息 TODO
	 * 
	 * @param id
	 * @return ResultDto
	 */
	@RequestMapping("/get/{id}")
	@ResponseBody
	public ResultDto get(@PathVariable String id) {
		ResultDto rtDto = new ResultDto();
		rtDto.setData(sysRoleService.get(id));
		return rtDto;
	}

}
